let fun={
    /**
     * 查询配置
     */
    queryProperties:()=>{
        fetch(window.server_path+window.server_post.queryProperties,{
            method:"POST",
            headers:{"Content-Type":"application/json"},
            body:JSON.stringify({
                phone:window.sessionStorage.getItem("phone"),
                command:window.localStorage.getItem("command")
            })
        }).then(re=>{return re.json()}).then(da=>{
            if(da.status){
                document.getElementById("nuNickname").value=da.data.nuNickname;
                document.getElementById("nuEnergy").value=da.data.nuEnergy;
                document.getElementById("expendEnergy").value=da.data.expendEnergy;
                document.getElementById("proEnergy").value=da.data.proEnergy;
                document.getElementById("proEnergyAward").value=da.data.proEnergyAward;
                document.getElementById("proGoods").value=da.data.proGoods;
                document.getElementById("proPet").value=da.data.proPet;
                document.getElementById("proBusinessman").value=da.data.proBusinessman;
                document.getElementById("proPlayer").value=da.data.proPlayer;
                let p=document.getElementById("nuPet");
                for(let i=0;i<p.options.length;i++){
                    if(p.options[i].id===da.data.nuPet){
                        p.selectedIndex=i;
                        break;
                    }
                }
            }else window.crlog.error(da.tip);
        })
    },
    /**
     * 查询宠物kv
     */
    queryPetOptions:()=>{
        fetch(window.server_path+window.server_post.queryPetOptions,{
            method:"POST",
            headers:{"Content-Type":"application/json"},
            body:JSON.stringify({
                phone:window.sessionStorage.getItem("phone"),
                command:window.localStorage.getItem("command")
            })
        }).then(re=>{return re.json()}).then(da=>{
            if(da.status){
                let a=document.getElementById("nuPet");
                a.innerHTML="";
                for(let i in da.data){
                    let op=document.createElement("option");
                    op.innerHTML=da.data[i].value;
                    op.id=da.data[i].key;
                    a.appendChild(op);
                }
            }else window.crlog.error(da.tip);
        })
    },
    updatePlayerProperties:()=>{
        let nuNickname=document.getElementById("nuNickname").value;
        let nuEnergy=document.getElementById("nuEnergy").value;
        let nuPet=document.getElementById("nuPet");
        nuPet=nuPet.options[nuPet.selectedIndex].id;
        if(nuNickname.length<=0){
            window.crlog.warning("请输入昵称");
            return;
        }
        if(nuNickname.replace("{uid}","123456").length>24){
            window.crlog.warning("昵称长度不得超过24字符");
            return;
        }
        if(!/^[A-Za-z0-9\u4e00-\u9fa5]+$/.test(nuNickname.replace("{uid}",""))){
            window.crlog.warning("昵称只能包含数字、字母和中文");
            return;
        }
        if(nuEnergy.length<=0){
            window.crlog.warning("请输入能量");
            return;
        }
        fetch(window.server_path+window.server_post.updatePlayerProperties,{
            method:"POST",
            headers:{"Content-Type":"application/json"},
            body:JSON.stringify({
                phone:window.sessionStorage.getItem("phone"),
                command:window.localStorage.getItem("command"),
                nuNickname:nuNickname,
                nuEnergy:nuEnergy,
                nuPet:nuPet
            })
        }).then(re=>{return re.json();}).then(da=>{
            console.log(da);
            if(da.status)window.crlog.success(da.tip);
            else window.crlog.error(da.tip);
        })
    },
    updateScanProperties: ()=>{
        let expendEnergy=document.getElementById("expendEnergy").value;
        let proEnergy=document.getElementById("proEnergy").value;
        let proEnergyAward=document.getElementById("proEnergyAward").value;
        let proGoods=document.getElementById("proGoods").value;
        let proPet=document.getElementById("proPet").value;
        let proBusinessman=document.getElementById("proBusinessman").value;
        let proPlayer=document.getElementById("proPlayer").value;
        if(expendEnergy.length<=0){
            window.crlog.warning("请输入搜寻消耗能量值");
            return;
        }
        if(proEnergy.length<=0){
            window.crlog.warning("请输入搜到能量概率");
            return;
        }
        if(proEnergyAward.length<=0){
            window.crlog.warning("请输入搜到能量奖励数值");
            return;
        }
        if(proGoods.length<=0){
            window.crlog.warning("请输入搜到物品概率");
            return;
        }
        if(proPet.length<=0){
            window.crlog.warning("请输入搜到宠物概率");
            return;
        }
        if(proBusinessman.length<=0){
            window.crlog.warning("请输入搜到商人概率");
            return;
        }
        if(proPlayer.length<=0){
            window.crlog.warning("请输入搜到玩家概率");
            return;
        }
        fetch(window.server_path+window.server_post.updateScanProperties,{
            method:"POST",
            headers:{"Content-Type":"application/json"},
            body:JSON.stringify({
                phone:window.sessionStorage.getItem("phone"),
                command:window.localStorage.getItem("command"),
                expendEnergy:expendEnergy,
                proBusinessman:proBusinessman,
                proEnergy:proEnergy,
                proPlayer:proPlayer,
                proGoods:proGoods,
                proEnergyAward:proEnergyAward,
                proPet:proPet
            })
        }).then(re=>{return re.json();}).then(da=>{
            if(da.status)window.crlog.success(da.tip);
            else window.crlog.error(da.tip);
        })
    },
    openImg:()=>{
        let fc=document.createElement("input");
        fc.type="file";
        fc.accept=".jpg,.png";
        fc.onchange=e=>{
            let f=e.target.files[0];
            if(f===null)return;
            let fr=new FileReader();
            fr.onload=ee=>{
                let im=new Image();
                im.onload=eee=>{
                    let can=document.createElement("canvas");
                    can.width=750;
                    can.height=750/im.width*im.height;
                    can.style.width="750px";
                    can.style.height=can.height+"px";
                    let g=can.getContext("2d");
                    g.drawImage(im,0,0,can.width,can.height);
                    document.getElementById("background_img").src=can.toDataURL("image/png");
                }
                im.src=ee.target.result;
            }
            fr.readAsDataURL(f);
        }
        fc.click();
    },
    updateLoadingImg:()=>{
        let data=document.getElementById("background_img").src;
        if(data.substring(0,22)!=="data:image/png;base64,"){
            window.crlog.warning("未选择图片");
            return;
        }
        fetch(window.server_path+window.server_post.updateLoadingImg,{
            method:"POST",
            headers:{"Content-Type":"application/json"},
            body:JSON.stringify({
                data:JSON.stringify({
                    phone:window.sessionStorage.getItem("phone"),
                    command:window.localStorage.getItem("command"),
                    data:data.substring(22)
                })
            })
        }).then(re=>{return re.json()}).then(da=>{
            if(!da.status){
                window.crlog.error(da.tip);
                return;
            }
            window.crlog.success("更改成功");
            document.getElementById("background_img").src=window.resource_path.other+"loading.png";
        })
    },
    playBackgroundBgm:(e)=>{
        let audio=document.getElementById("background_bgm");
        if(audio.ended || audio.paused){
            audio.play();
            e.innerHTML="停止播放";
            audio.onended=ee=>{
                audio.currentTime=0;
                e.innerHTML="播放试听";
            }
        }else{
            audio.pause();
            audio.currentTime=0;
            e.innerHTML="播放试听";
        }
    },
    openBackgroundBgm:()=>{
        let fc=document.createElement("input");
        fc.type="file";
        fc.accept=".mp3";
        fc.onchange=e=>{
            let f=e.target.files[0];
            if(f===null)return;
            let fr=new FileReader();
            fr.onload=ee=>{
                let audio=document.getElementById("background_bgm");
                audio.src=ee.target.result;
            }
            fr.readAsDataURL(f);
        }
        fc.click();
    },
    updateBackgroundBgm:()=>{
        let data=document.getElementById("background_bgm").src;
        if(data.substring(0,23)!=="data:audio/mpeg;base64,"){
            window.crlog.warning("未选择背景音乐");
            return;
        }
        fetch(window.server_path+window.server_post.updateBackgroundBgm,{
            method:"POST",
            headers:{"Content-Type":"application/json"},
            body:JSON.stringify({
                data:JSON.stringify({
                    phone:window.sessionStorage.getItem("phone"),
                    command:window.localStorage.getItem("command"),
                    data:data.substring(23)
                })
            })
        }).then(re=>{return re.json()}).then(da=>{
            if(!da.status){
                window.crlog.error(da.tip);
                return;
            }
            window.crlog.success("更换背景音乐成功");
            document.getElementById("background_bgm").src=window.resource_path.other+"bgm.mp3";
        })
    }
}
fun.queryPetOptions();
document.getElementById("background_img").src=window.resource_path.other+"loading.png";
document.getElementById("background_bgm").src=window.resource_path.other+"bgm.mp3";
setTimeout(fun.queryProperties,200);